package com.cloud.manager.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.manager.core.exception.ServiceException;
import com.cloud.manager.utils.Md5Utils;
import com.cloud.manager.web.domain.User;
import com.cloud.manager.web.service.UserService;
import com.cloud.manager.web.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author FineEx
 * @description 针对表【tm_user】的数据库操作Service实现
 * @createDate 2021-11-19 15:16:12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Resource
    private UserMapper userMapper;


    @Override
    public Page<User> selectUserAll(int currentPage, int pageSize) {
        // 创建分页对象
        Page<User> page = new Page<>(currentPage, pageSize);
        // 调用 mybatis-plus 提供的分页查询方法
        Page<User> userPage = userMapper.selectPage(page, null);
        return userPage;
    }

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public User login(String userName, String password) {
        User user;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userName", userName);
        wrapper.eq("password", Md5Utils.hash(password));
        user = userMapper.selectOne(wrapper);
        if (user == null) {
            throw new ServiceException("用户名或密码错误！");
        }
//        if (!user.isEnable()) {
//            throw new ServiceException("账号已锁定，请联系客服解锁！");
//        }
        return user;
    }

    @Override
    public int disableUser(String userId) {
        User user;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", userId);
        user = userMapper.selectOne(wrapper);
        if (user == null) {
            throw new ServiceException("用户不存在！");
        }
        if (!user.isEnable()) {
            throw new ServiceException("用户已禁用，请勿重复禁用！");
        }
        user.setEnable(false);
        return userMapper.updateById(user);
    }
}




